package projectEuler.net;

public class Problem_046 {
	private static boolean isTwiceSquare(int number){
		double tmp = Math.sqrt(number/2);
		return (tmp==(int)tmp);
	}
	private static boolean isCompositeNumber(int number){
		int lstPrimes[] = createListOfPrimesLeesX((int)Math.sqrt(number));
		for(int i=0; i<lstPrimes.length; i++)
			if(number%lstPrimes[i] == 0) return true;
		return false;
	}
	public static int[] createListOfPrimesLeesX(int X){
		boolean[] unCrossed = new boolean[X+1];
		int cnt = 0;
		for(int i=0; i<=X; i++) unCrossed[i]=true;
		unCrossed[0]=unCrossed[1]=false;
		for(int i=2; i<Math.sqrt(unCrossed.length); i++)
			if(unCrossed[i])
				for(int j=2*i; j<unCrossed.length; j+=i) unCrossed[j]=false;
		for(int i=0; i<=X; i++)
			if(unCrossed[i]) cnt++;
		int lstResult[] = new int[cnt];
		cnt = 0;
		for(int i =0; i<=X; i++)
			if(unCrossed[i])lstResult[cnt++] = i;
		return lstResult;
	}
	public static int smallestNotGoldback(){
		boolean isBreak = false;
		for(int i=9;; i+=2)
			if(isCompositeNumber(i)){
				int lstPrimes[] = createListOfPrimesLeesX(i);
				for(int j=0; j<lstPrimes.length; j++)
					if(isTwiceSquare(i-lstPrimes[j])){
						isBreak = true;
						break;
					}else isBreak = false;
				if(!isBreak)
					return i;
			}
	}
}
